<!--
 * @Description: 砍价活动黑名单
 * @Author: liyujie
 * @Date: 2021/2/23
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/2/23
-->
<template>
    <div class="wrapper">
        <div class="content-wrapper">
            <!-- header -->
            <div class="header">
                <div class="header-title">
                    黑名单用户
                </div>
                <div class="add-btn">
                    <el-button
                        type="primary"
                        size="medium"
                        icon="el-icon-plus"
                        @click="showAddDialog"
                    >
                        添加用户
                    </el-button>
                </div>
            </div>
            <!-- /header -->
            <div class="filter-view">
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        用户姓名/编号/电话
                    </p>
                    <el-input
                        placeholder="搜索用户姓名/编号/电话"
                        v-model="filterData.keyword"
                        suffix-icon="el-icon-search"
                        clearable
                        size="medium"
                        style="width: 240px"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="getTableData(1)"
                    >
                        查询
                    </el-button>
                </div>
            </div>
            <!-- table -->
            <div class="table-wrapper">
                <el-table
                    :data="tableData"
                    height="100%"
                    style="width: 100%;"
                    ref="tempLateName"
                    class="data-table"
                    v-loading="tableLoading"
                >
                    <el-table-column
                        label="序号"
                        align="center"
                        width="80"
                        type="index"
                    />
                    <el-table-column
                        prop="true_name"
                        label="用户昵称"
                        show-overflow-tooltip
                        align="left"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        prop="student_no"
                        label="用户编号"
                        show-overflow-tooltip
                        align="center"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        prop="phone"
                        label="用户电话"
                        show-overflow-tooltip
                        align="center"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        prop="invite_code"
                        label="邀请码"
                        show-overflow-tooltip
                        align="center"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        label="是否发起砍价活动"
                        align="center"
                        :formatter="emptyFormatter"
                    >
                        <template slot-scope="scope">
                            <div>{{ scope.row.is_apply ? '是' : '否' }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="是否帮忙砍价"
                        align="center"
                        :formatter="emptyFormatter"
                    >
                        <template slot-scope="scope">
                            <div>{{ scope.row.is_help ? '是' : '否' }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="操作"
                        align="left"
                        width="140"
                    >
                        <template slot-scope="scope">
                            <div class="option-btn-group">
                                <el-button
                                    type="text"
                                    size="medium"
                                    v-if="scope.row.status"
                                    @click="setBlacklist(0, scope.row)"
                                >
                                    移除黑名单
                                </el-button>
                                <el-button
                                    type="text"
                                    size="medium"
                                    v-else
                                    @click="setBlacklist(1, scope.row)"
                                >
                                    加入黑名单
                                </el-button>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <!-- /table -->

            <!-- /分页 -->
            <div class="pagination-wrapper clearfix">
                <el-pagination
                    layout="total, prev, pager, next, jumper"
                    background
                    :current-page.sync="page.now_page"
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getTableData"
                />
            </div>
        </div>

        <el-dialog
            title="添加用户"
            :visible.sync="dialogVisible"
            width="680px"
            :close-on-click-modal="false"
            @close="handleClose"
        >
            <div>
                <!-- 筛选-->
                <div class="filter-wrap">
                    <div class="header-new-box">
                        <p class="header-new-box-title">请输入用户的编号/名称/邀请码：</p>
                        <el-input
                            class="input-with-select"
                            placeholder="请输入用户的编号/名称/邀请码"
                            v-model="userFilter.keyword"
                            clearable
                            size="small"
                            @keydown.enter.native="getUserTableData(1)"
                        />
                    </div>
                    <el-button
                        type="primary"
                        size="small"
                        class="search-btn"
                        @click="getUserTableData(1)"
                    >
                        搜索
                    </el-button>
                </div>
                <!-- table -->
                <el-table
                    :data="userTableData"
                    max-height="438"
                    style="width: 100%;"
                    class="data-table"
                    v-if="userTableData.length"
                >
                    <el-table-column
                        prop="true_name"
                        label="用户名"
                        align="center"
                        show-overflow-tooltip
                    />
                    <el-table-column
                        prop="phone"
                        label="用户手机号"
                        align="center"
                    />
                    <el-table-column
                        prop="student_no"
                        label="用户编号"
                        align="center"
                        show-overflow-tooltip
                    />
                    <el-table-column
                        prop="invite_code"
                        label="邀请码"
                        align="center"
                        show-overflow-tooltip
                    />
                    <el-table-column
                        label="操作"
                        align="center"
                    >
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                @click="handleSelect(scope.row)"
                            >
                                选择
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!-- /分页 -->
                <div
                    class="pagination-wrapper clearfix"
                    style="text-align: right;margin-top: 24px"
                    v-if="userTableData.length"
                >
                    <el-pagination
                        layout="total, prev, pager, next, jumper"
                        background
                        :current-page.sync="userPage.now_page"
                        :total="userPage.total_count"
                        :page-size="userPage.page_size"
                        @current-change="getTableData"
                    />
                </div>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { back } from '@/mixin/back';

export default {
    name: 'BargainActivityBlacklist',
    mixins: [back],
    components: {},
    data() {
        return {
            filterData: {
                keyword: '',
            },
            userFilter: {
                keyword: ''
            },
            tableData: [],
            tableLoading: false,
            userTableData: [],
            userPage: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            dialogVisible: false,
            authResult: {}
        };
    },
    computed: {},
    watch: {},
    filters: {
        statusFilter(val) {
            switch (val) {
                case -1:
                    return '失败';
                case 0:
                    return '进行中';
                case 1:
                    return '成功';
                default:
                    return '--';
            }
        },
    },
    mounted() {
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '商城'}, {title: '砍价活动'},{title: '发起人管理'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },

    methods: {

        /**
         * @description 获取列表数据
         * @param page
         */
        getTableData(page) {
            this.tableLoading = true;
            let params = { ...this.filterData };

            params.page_id = page || this.page.now_page;
            this.$post('/student/shop_tool_bargain%5Cstudent_block%5Cget_list', params, res => {
                if (res.code === 1) {
                    this.tableData = res.data.list;
                    this.page = res.data.page;
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
                this.tableLoading = false;
            });
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         */
        handleRoute(routeName, query) {
            this.$router.push({
                name: routeName,
                query
            });
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 加入移除黑名单
         * @param status 1加入 0移除
         * @param row
         */
        setBlacklist(status, row) {
            if(!this.authResult.auth_set) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }

            this.$confirm(`确认要${status ? `加入` : '移除'}黑名单吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = {
                    student_no: row.student_no,
                    status
                };

                this.$post('/student/shop_tool_bargain%5Cstudent_block%5Cset_status', params, resp => {
                    if (resp.code === 1) {
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            message: '操作成功',
                            duration: 2000,
                            type: 'success'
                        });
                    } else {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            duration: 2000,
                            type: 'warning'
                        });
                    }
                });
            });
        },


        /**
         * 获取学生列表
         */
        getUserTableData() {
            let params = {...this.userFilter};

            this.$post('/student/shop_tool_bargain%5Cstudent_block%5Cget_student_list', params, resp => {
                if (resp.code === 1) {
                    this.userTableData = resp.data.list;
                    this.userPage = resp.data.page;
                    if(resp.data.list.length === 0) {
                        this.$message.warning('查询结果为空');
                    }
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * 弹窗关闭
         */
        handleClose() {
            this.filterData.keyword = '';
            this.userTableData = [];
        },

        /**
         * 选择
         * @param row
         */
        handleSelect(row) {
            this.$post('/student/shop_tool_bargain%5Cstudent_block%5Cadd', {student_no: row.student_no}, resp => {
                if (resp.code === 1) {
                    this.dialogVisible = false;
                    this.getTableData();
                    this.$notify({
                        title: '提示',
                        message: '操作成功',
                        duration: 2000,
                        type: 'success'
                    });
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * 添加用户
         */
        showAddDialog() {
            if(!this.authResult.auth_add) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.dialogVisible = true;
        }
    }
};
</script>

<style lang="less" scoped>
.wrapper {
    width: 100%;

    .content-wrapper {
        display: flex;
        height: calc(100% - 24px);
        flex-direction: column;
        position: relative;
        margin: 12px;
        background: #fff;
        box-sizing: border-box;

        /*header*/

        .header {
            height: 68px;
            padding: 24px;
            box-sizing: border-box;
            box-shadow: 0px -1px 0px 0px #F0F2F5;
            display: flex;
            align-items: center;
            border-bottom: 1px solid #F0F2F5;
            justify-content: space-between;

            .header-title {
                color: #333333;
                font-size: 20px;
                height: 20px;
                font-weight: 500;
                position: relative;
                margin: 24px 0 24px 12px;

                &::before {
                    content: '';
                    position: absolute;
                    left: -8px;
                    top: -1px;
                    width: 3px;
                    height: 20px;
                    background: #1890FF;
                }
            }
        }

        /*筛选*/

        .filter-view {
            margin-top: 24px;
            padding: 0 24px;
            box-sizing: border-box;
            display: flex;
            align-items: center;
            flex-wrap: wrap;

            .header-new-box {
                display: flex;
                align-items: center;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #333333;
                white-space: nowrap;
                margin-right: 24px;
                margin-bottom: 24px;

                &-title {
                    margin-right: 16px;
                    white-space: nowrap;
                }
            }
        }
        // 表格
        .table-wrapper {
            padding: 0 24px;
            flex: 1;
            height: 0;
        }

        /* 分页 */

        .pagination-wrapper {
            padding: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow:0 1px 0 0 rgba(233,233,233,1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;
        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }
        .el-icon-close:before {
            color: rgb(194,194,194);
            font-size: 17px;
            font-weight: bold;
        }
        .el-dialog__headerbtn {
            top: 14px;
        }
    }
    /deep/ .el-textarea__inner {
        resize: none;
    }
    .filter-wrap {
        padding: 0 24px;
        margin: 6px 0 24px;
        box-sizing: border-box;
        display: flex;
        justify-content: center;
        align-items: center;

        .header-new-box {
            display: flex;
            align-items: center;
            color: #606266;
            white-space: nowrap;

            &-title {
                margin-right: 2px;
                white-space: nowrap;
            }
        }

        .search-btn {
            border-radius: 0 4px 4px 0;
        }
        .input-with-select {
            width: 300px;
            /deep/ .el-input__inner {
                border-radius: 4px 0 0 4px;
                border-right: 0;
            }
        }
    }
    // 表格
    .data-table {
        ::-webkit-scrollbar {
            width: 6px;
            height: 6px;
        }
        // 操作按钮
        .option-btn-group {
            .el-button {
                margin-left: 0;
                margin-right: 10px;
            }
        }
    }
}
</style>

